/*
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2022-06-24 09:16:47
 * @LastEditTime: 2022-06-24 09:25:48
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description:
 * @FilePath: \vue3-element-plus-web\src\utils\crypto.js
 * @Copyright 版权声明
 */
import CryptoJS from 'crypto-js'
import { AES_KEY } from '@/config'

/**
 * aes 加密方法，同java：AesUtil.encryptToBase64(text, aesKey);
 */
export const encryptAES = (data) => {
  const dataBytes = CryptoJS.enc.Utf8.parse(data)
  const keyBytes = CryptoJS.enc.Utf8.parse(AES_KEY)
  const encrypted = CryptoJS.AES.encrypt(dataBytes, keyBytes, {
    iv: keyBytes,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
}

/**
 * aes 解密方法，同java：AesUtil.decryptFormBase64ToString(encrypt, aesKey);
 */
export const decryptAES = (data) => {
  const keyBytes = CryptoJS.enc.Utf8.parse(AES_KEY)
  const decrypted = CryptoJS.AES.decrypt(data, keyBytes, {
    iv: keyBytes,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  return CryptoJS.enc.Utf8.stringify(decrypted)
}
